#include "UDPSocketThread.h"
#include <QTimer>

UDPSocketThread::UDPSocketThread(QObject* parent)
    : QThread(parent), worker(nullptr) {
}

UDPSocketThread::~UDPSocketThread() {
    quit();
    wait();
}

void UDPSocketThread::run() {
    worker = new UDPSocketWorker();
    worker->moveToThread(this);
    connect(this, &QThread::finished, worker, &QObject::deleteLater);

    // 初始化 socket
    QMetaObject::invokeMethod(worker, "process", Qt::QueuedConnection);

    // 启动定时发送Ping
    QTimer pingTimer;
    connect(&pingTimer, &QTimer::timeout, worker, &UDPSocketWorker::sendPing);
    pingTimer.start(1000);  // 每秒发送一次

    // 启动发送线程
    send = new SendThread();
    connect(send, &SendThread::messageReady, worker, &UDPSocketWorker::sendMessage);
    send->start();

    exec();
}
